package com.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.pojo.MovieList;
import com.pojo.Special;
import com.service.SpecialFilmListService;
import com.service.SpecialService;
import com.util.PortUtil;
import com.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

@RestController
@RequestMapping("/api/v1")
public class SpecialAction {

    @Autowired
    private SpecialService specialService;

    @Autowired
    private SpecialFilmListService specialFilmListService;

    @Value("${server.url}")
    private String server;

    @Value("${server.port}")
    private String port;

    @Value("${dy.home}")
    private String home;

    /**
     * 获取所有可用专题
     * @return
     */
    @RequestMapping("/getAllSpecial")
    public ResponseBean getAllSpecial() {
        List<Special> list = specialService.getAll();
        if (list == null || list.size() == 0) {
            return new ResponseBean(404, "找不到专题", null);
        }

        String ip = server;
        InputStream is = null;
        try {
            is = new FileInputStream("ip.properties");
            Properties p = new Properties();
            p.load(is);
            String ip2 = p.getProperty("server.ip");
            if (StringUtils.isEmpty(ip2)==false)
            {
                ip = "http://"+ip2+":"+port+"/";
            }

        } catch (Exception e) {

        } finally {
            try {
                is.close();
            } catch (IOException e) {
                // e.printStackTrace();
            }
        }
        if (ip.endsWith("/"))
        {
            ip = ip.substring(0,ip.length()-1);
        }

        for (Special s:list)
        {
            String backPic = s.getBackground_picture();
            if (backPic!=null && backPic.startsWith("http://")==false)
            {
                s.setBackground_picture(ip+backPic);
            }
            String pic = s.getPicture();
            if (pic!=null && pic.startsWith("http://")==false)
            {
                s.setPicture(ip+pic);
            }
        }

        return new ResponseBean(200, "获取成功", list);
    }

    /**
     * 根据专题获取电影
     * @param id
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("/getFilmBySpecial")
    public ResponseBean getFilmBySpecial(int id, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "20")
            int size) {
        IPage result = specialFilmListService.getSpecialFilmByTypeId(id, page, size);
        long total = result.getTotal();
        if (total == 0) {
            return new ResponseBean(404, "没有相关电影", null);
        }
        String tempPort = port;
        if (PortUtil.checkPortIsOpen(8090))
        {
            tempPort = "8090";
        }
        //----------------------电影图片-------------------
        String ip = server;
        InputStream is = null;
        try {
            is = new FileInputStream("ip.properties");
            Properties p = new Properties();
            p.load(is);
            String ip2 = p.getProperty("server.ip");
            if (StringUtils.isEmpty(ip2)==false)
            {
                ip = "http://"+ip2+":"+tempPort+"/";
            }

        } catch (Exception e) {

        } finally {
            try {
                is.close();
            } catch (IOException e) {
                // e.printStackTrace();
            }
        }
        if (ip.endsWith("/"))
        {
            ip = ip.substring(0,ip.length()-1);
        }

        List<MovieList> list = result.getRecords();
        for (MovieList m : list) {
            String bigImage = m.getBig_poster();
            String smallImage = m.getLittle_poster();
            if (bigImage!=null && bigImage.startsWith("http://")==false)
            {
                m.setBig_poster(ip + bigImage);
            }
            if (smallImage!=null && smallImage.startsWith("http://")==false)
            {
                m.setLittle_poster(ip + smallImage);
            }
            String local_img = m.getLocal_img();
            if (local_img!=null){
                File localImgFile= new File(home+"images/",local_img);
                if (localImgFile.exists()){
                    m.setBig_poster(ip + "/images/"+local_img);
                    m.setLittle_poster(ip + "/images/"+local_img);
                }
            }
        }


        return new ResponseBean(200, total, list);
    }

}
